home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / TERMLIB / TESTTCP.C < prev    next >
C/C++ Source or Header  |  1988-08-23  |  8KB  |  372 lines

  1. /************************************************************************
  2.  *                                      *
  3.  *              Copyright (c) 1982, Fred Fish              *
  4.  *              All Rights Reserved                  *
  5.  *                                      *
  6.  *    This software and/or documentation is released for public          *
  7.  *    distribution for personal, non-commercial use only.          *
  8.  *    Limited rights to use, modify, and redistribute are hereby      *
  9.  *    granted for non-commercial purposes, provided that all          *
  10.  *    copyright notices remain intact and all changes are clearly     *
  11.  *    documented.  The author makes no warranty of any kind with      *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular     *
  14.  *    purpose.                                  *
  15.  *                                      *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20.  
  21.  
  22. /*
  23.  *  TEST PROGRAM
  24.  *
  25.  *    testtcp    test termcap functions
  26.  *
  27.  *  KEY WORDS
  28.  *
  29.  *    test routines
  30.  *    termcap test
  31.  *
  32.  *  SYNOPSIS
  33.  *
  34.  *    termcap [-efns] terminal [capability [capability ...]]
  35.  *
  36.  *          -e  =>   expand string capability given by -s
  37.  *          -f  =>   determine boolean capabilities for terminal
  38.  *          -n  =>   determine numeric capabilities for terminal
  39.  *          -s  =>   determine string capabilities for terminal
  40.  *
  41.  *          terminal =>  terminal name as given in termcap file
  42.  *          capability => a boolean, numeric, or string capability
  43.  *
  44.  *          NOTE:  All capabilities must be of same type, as
  45.  *             given by [-fns].
  46.  *
  47.  *          If terminal is only argument then entire entry is
  48.  *          printed.
  49.  *
  50.  *  DESCRIPTION
  51.  *
  52.  *    Provides way to test termcap functions.  Can find
  53.  *    and print an entire termcap terminal entry, or various
  54.  *    capabilities from the entry.
  55.  *
  56.  *  AUTHOR
  57.  *
  58.  *    Fred Fish
  59.  *
  60.  */
  61.  
  62. #include <stdio.h>
  63.  
  64. #define TRUE 1
  65. #define FALSE 0
  66. #define NO_FILE           -1              /* Returned if can't open file */
  67. #define NO_ENTRY  0              /* Returned if can't find entry */
  68. #define SUCCESS      1              /* Returned if entry found ok */
  69. #define TRUNCATED 2              /* Returned if entry found but trunc */
  70. #define BUFFER_SIZE 1024
  71.  
  72. int eflag = FALSE;
  73. int fflag = FALSE;
  74. int nflag = FALSE;
  75. int sflag = FALSE;
  76.  
  77. int got_terminal = FALSE;
  78. int got_capability = FALSE;
  79.  
  80.  
  81.  
  82.  
  83. /*
  84.  *  FUNCTION
  85.  *
  86.  *    main   termcap test entry point
  87.  *
  88.  *  KEY WORDS
  89.  *
  90.  *    main
  91.  *
  92.  *  SYNOPSIS
  93.  *
  94.  *    main(argc,argv)
  95.  *    int argc;
  96.  *    char *argv[];
  97.  *
  98.  *  DESCRIPTION
  99.  *
  100.  *    This is where the termcap test starts executing.    All argument list
  101.  *    switches are processed first, then all the specified
  102.  *    capability identification strings are processed.
  103.  *
  104.  */
  105.  
  106.  
  107.  
  108. /*
  109.  *  PSEUDO CODE
  110.  *
  111.  *    Begin main
  112.  *      Process command line options.
  113.  *      For each argument list field
  114.  *          If field was not erased during option processing
  115.  *          If terminal name field not yet processed then
  116.  *              Process an assumed terminal name field.
  117.  *              Set terminal name processed flag.
  118.  *          Else
  119.  *              Process a capability field.
  120.  *              Set capability field processed flag.
  121.  *          End if
  122.  *          End if
  123.  *      End for
  124.  *      If no capabilities processed then
  125.  *          Simply dump buffer.
  126.  *      End if
  127.  *    End main
  128.  *
  129.  */
  130.  
  131. main(argc, argv)
  132. int argc;
  133. char *argv[];
  134. {
  135.     char *argp;
  136.     int argnum;
  137.     char buffer[BUFFER_SIZE];
  138.  
  139.     options(argc,argv);
  140.     for (argnum = 1; argnum < argc; argnum++) {
  141.     if ((argp = argv[argnum]) != NULL) {
  142.       if (!got_terminal) {
  143.           terminal(buffer,argp);
  144.           got_terminal = TRUE;
  145.       } else {
  146.           capability(argp);
  147.           got_capability = TRUE;
  148.       }
  149.     }
  150.     }
  151.     if (got_terminal && !got_capability) {
  152.       printf("%s",buffer);
  153.     }
  154. }
  155.  
  156.  
  157.  
  158. /*
  159.  *  FUNCTION
  160.  *
  161.  *    options    process command line options
  162.  *
  163.  *  SYNOPSIS
  164.  *
  165.  *    options(argc,argv)
  166.  *    int argc;
  167.  *    char *argv[];
  168.  *
  169.  *  DESCRIPTION
  170.  *
  171.  *    Scans argument list, processing each switch as it is
  172.  *    found.  The pointer to each switch string is then
  173.  *    replaced with a NULL to effectively erase the switch
  174.  *    argument.
  175.  *
  176.  */
  177.  
  178.  
  179.  
  180. /*
  181.  *  PSEUDO CODE
  182.  *
  183.  *    Begin options
  184.  *      For each argument in the argument list
  185.  *          Get pointer to first char of argument.
  186.  *          If the argument is a switch then
  187.  *          Replace argument pointer with NULL.
  188.  *          Look at next argument character.
  189.  *          While there is another argument character
  190.  *              Switch on the argument character
  191.  *              Case "EXPAND":
  192.  *              Set expand (e) flag.
  193.  *              Break out of switch.
  194.  *              Case "BOOLEAN":
  195.  *              Set boolean (f) flag.
  196.  *              Break out of switch.
  197.  *              Case "NUMERIC":
  198.  *              Set numeric flag.
  199.  *              Break out of switch.
  200.  *              Case "STRING":
  201.  *              Set string flag.
  202.  *              Break out of switch.
  203.  *              Default:
  204.  *              Abort with usage message.
  205.  *              End switch
  206.  *          End while
  207.  *          End if
  208.  *      End for
  209.  *    End options
  210.  *
  211.  */
  212.  
  213.  
  214.  
  215. options(argc, argv)
  216. int argc;
  217. char *argv[];
  218. {
  219.     int i;
  220.     char c;          /* 1st char of current command-line argument */
  221.     char *cp;          /* current argument pointer */
  222.  
  223.     for (i=1; i<argc; i++) {
  224.     cp = argv[i];
  225.     if (*cp == '-') {
  226.         argv[i] = NULL;
  227.       cp++;
  228.       while (c = *cp++) {
  229.           switch (c) {
  230.           case 'e':
  231.           eflag = TRUE;
  232.           break;
  233.           case 'f':
  234.           fflag = TRUE;
  235.           break;
  236.           case 'n':
  237.           nflag = TRUE;
  238.           break;
  239.           case 's':
  240.           sflag = TRUE;
  241.           break;
  242.           default:
  243.           usage();
  244.           }
  245.         }
  246.     }
  247.     }
  248. }
  249.  
  250.  
  251.  
  252. /*
  253.  *  FUNCTION
  254.  *
  255.  *    usage   give usage message and abort
  256.  *
  257.  *  KEY WORDS
  258.  *
  259.  *    usage
  260.  *    help processing
  261.  *    abort locations
  262.  *
  263.  *  SYNOPSIS
  264.  *
  265.  *    usage()
  266.  *
  267.  *  DESCRIPTION
  268.  *
  269.  *    Usage is typically called when a problem has been
  270.  *    detected in the argument list.
  271.  *    It prints a usage message and exits.
  272.  *
  273.  */
  274.  
  275.  
  276.  
  277. /*
  278.  *  PSEUDO CODE
  279.  *
  280.  *    Begin usage
  281.  *      Print usage message.
  282.  *      Exit.
  283.  *    End usage
  284.  *
  285.  */
  286.  
  287. usage()
  288. {
  289.     printf("Usage: termcap [-fns] terminal [capability [capability ... ]]\n");
  290.     exit();
  291. }
  292.  
  293.  
  294.  
  295.  
  296. terminal(buffer,name)
  297. char *buffer;
  298. char *name;
  299. {
  300.     int status;
  301.  
  302.     status = tgetent(buffer,name);
  303.     switch (status) {
  304.     case NO_FILE:
  305.       fprintf(stderr,"Can't find a termcap data base file.\n");
  306.       exit();
  307.     case NO_ENTRY:
  308.       fprintf(stderr,"Can't find entry \"%s\"\n",name);
  309.       exit();
  310.     case TRUNCATED:
  311.       fprintf(stderr,"Warning --- entry \"%s\" too long\n",name);
  312.       break;
  313.     case SUCCESS:
  314.     break;
  315.     default:
  316.     fprintf(stderr,"? tgetent returned illegal status %d\n",status);
  317.       exit();
  318.     }
  319. }
  320.  
  321.  
  322.  
  323. capability(id)
  324. char *id;
  325. {
  326.     int value;
  327.     char buffer[256];
  328.     char *area;
  329.     char *ep, *tgoto();
  330.  
  331.     if (fflag) {
  332.       value = tgetflag(id);
  333.       if (value) {
  334.       printf("%s TRUE\n",id);
  335.       } else {
  336.       printf("%s FALSE\n",id);
  337.       }
  338.     } else if (nflag) {
  339.       value = tgetnum(id);
  340.       printf("%s = %o octal %d decimal\n",id,value,value);
  341.     } else if (sflag) {
  342.       area = buffer;
  343.       tgetstr(id,&area);
  344.       if (eflag) {
  345.       ep = tgoto(buffer,75,23);
  346.       }
  347.       doprint(id,buffer);
  348.       if (eflag) {
  349.       doprint(id,ep);
  350.       ep = tgoto(buffer,1,2);
  351.       doprint(id,ep);
  352.       }
  353.     }
  354. }
  355.  
  356.  
  357.  
  358. doprint(id,cp)
  359. char *id;
  360. char *cp;
  361. {
  362.     printf("%s = \"",id);
  363.     for ( ; *cp != NULL; cp++) {
  364.       if (*cp < 040) {
  365.       printf("^%c",*cp |= 0100);
  366.       } else {
  367.       printf("%c",*cp);
  368.       }
  369.     }
  370.     printf("\"\n");
  371. }
  372.